/**
 *
 * Copyright (c) 2012 NCS Corporation.
 *
 * Author: tangb
 * Created Date: July 23, 2012
 *
 * 	version					author					remark
 * 	1.0						tangb				
 */
package com.ocgs.code.tag;

import com.ocgs.base.core.exception.BaseServiceException;
import com.ocgs.code.model.Code;
import com.ocgs.code.service.CodeService;
import com.ocgs.code.service.impl.CodeServiceImpl;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Component;
import org.apache.struts2.components.Select;
import org.apache.struts2.views.jsp.ui.SelectTag;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@SuppressWarnings("serial")
public class CodeTypeSelect extends SelectTag {

	private static final Log log = LogFactory.getLog(CodeTypeSelect.class);

	private String cdTypeId;
	private String scope;
    private  CodeService codeService;
	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getCdTypeId() {
		return cdTypeId;
	}

	public void setCdTypeId(String cdTypeId) {
		this.cdTypeId = cdTypeId;
	}

    public CodeService getCodeService() {
        return codeService;
    }

    public void setCodeService(CodeService codeService) {
        this.codeService = codeService;
    }

    public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
		return new Select(stack, req, res);
	}

	protected void populateParams() {
		super.populateParams();
		Select field = ((Select) component);

		if (log.isDebugEnabled())
			log.info("scope: " + scope + " cdTypeId: " + cdTypeId);

        List<Code> list = null;
        ServletContext servletContext = ServletActionContext.getRequest().getSession().getServletContext(); ;
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        codeService = wac.getBean(CodeServiceImpl.class);
        try {
            list = codeService.findCodeList(cdTypeId);
        } catch (BaseServiceException e) {
            e.printStackTrace();
        }
		field.setList(list);
		field.setListKey("codeId");
		field.setListValue("codeDesc");
	}
}
